﻿Shader "Custom/TestShader24"
{
	Properties
	{
        _Color ("Color", color) = (1,1,1,1)
		_MainTex ("Texture", 2D) = "white" {}
        _Ramp("Ramp", 2D) = "white" {}
        _Outline("Outline", float) = 0.1
        _OutlineColor("OutlineColor", color) = (1,1,1,1)
        _Specular("Specular", color) = (1,1,1,1)
        _SpecularScale("SpecularScale", range(0,0.1)) = 0.01
    }
    SubShader
    {
         Pass
         {
            NAME "OUTLINE"

            Cull Front

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            float _Outline;
            fixed4 _OutlineColor;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
            };

            v2f vert(a2v v)
            {
                v2f o;
                float3 pos = UnityObjectToViewPos(v.vertex);
                float3 normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
                normal.z = -0.5;
                pos = pos + normalize(normal) * _Outline;
                o.pos = mul(UNITY_MATRIX_P, float4(pos, 1));
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return fixed4(_OutlineColor.rgb, 1);
            }
            
            ENDCG
        }

        Pass
        {
            Tags {"LightMode" = "ForwardBase"}
            Cull Back
            CGPROGRAM
            #pragma multi_compile_fwdbase;
            #pragma vertex vert
            #pragma fragment frag
            #include "AutoLight.cginc"
            #include "Lighting.cginc"

            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _Ramp;
            fixed4 _Specular;
            float _SpecularScale;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float3 worldPos : TEXCOORD2;
                SHADOW_COORDS(3)
            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

                TRANSFER_SHADOW(o)

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                half3 worldPos = i.worldPos;
                half3 worldNormal = normalize(i.worldNormal);
                half3 viewDir = UnityWorldSpaceViewDir(worldPos);
                half3 lightDir = UnityWorldSpaceLightDir(worldPos);

                fixed4 c = tex2D(_MainTex, i.uv);
                fixed3 albedo = c.rgb * _Color.rgb;

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

                fixed diff = dot(lightDir, worldNormal);
                diff = (0.5 + diff * 0.5) * atten;

                fixed3 diffuse = _LightColor0.rgb * albedo * tex2D(_Ramp, fixed2(diff, diff)).rgb;

                fixed3 worldhalf = normalize(worldNormal + viewDir);
                fixed spec = dot(worldNormal, worldhalf);

                fixed w = fwidth(spec) * 2;
                fixed3 specular = _Specular.rgb * lerp(0, 1, smoothstep(-w, w, spec + _SpecularScale - 1)) * step(0.0001, _SpecularScale);

                return fixed4(ambient + diffuse + specular, 1);

            }

            ENDCG
        }
	}
}
